﻿function Part(partID, PN , pqty) {
    this.PMID = partID;
    this.PN = PN;
    this.qty = pqty;
    this.asHTML = function () {
        return this.PN + " X " + this.qty;
    }
    this.asXML = function () {
        return "<component id='" + this.PMID + "' qty='" + this.qty + "' />";
    }
    this.isBOM = false;
}
function BOM(id, name, rev , extras) {
    this.ID = id;
    this.Name = name;
    this.Rev = rev;
    this.SalesQty = 0;
    this.LOH = 0;
    this.Profit = 0;
    this.Desc = "None Yet";
    this.Parts = new Array();
    this.Qty = 0;
    this.isBOM = true;
    for (i in extras) {
        this[i] = extras[i];
    }
    this.add = function (part) {
        this.Parts.push(part);
    }
    this.asHTML = function (child) {
        var retVal = new Array();
        retVal.push(this.Name);
        if (child) {
            retVal.push(" X " + this.Qty);
        }
        retVal.push("<ul>");
        for (var i = 0; i < this.Parts.length; i++) {
            retVal.push("<li "+(this.Parts[i].isBOM?"class='subAssy'":"")+" onclick='toggle(this)'>");
            retVal.push(this.Parts[i].asHTML(true));
            retVal.push("</li>");
        }
        retVal.push("</ul>");
        return retVal.join("");
    }
    this.asXML = function () {
        var retVal = new Array();
        retVal.push("<bom id='" + this.ID + "' name='" + this.Name + "' revision='" + this.Rev + "' salesqty='" + this.SalesQty + "' loh='" + this.LOH + "' profit='" + this.Profit + "' qty='"+this.Qty+"'>");
        retVal.push("<description>");
        retVal.push(this.Desc);
        retVal.push("</description>");
        for (var i = 0; i < this.Parts.length; i++) {
            retVal.push("<part>");
            retVal.push(this.Parts[i].asXML());
            retVal.push("</part>");
        }
        retVal.push("</bom>");
        return retVal.join("");
    }
}


var p1 = new Part(1,"123",2);
var p2 = new Part(2,"223",4);
var p3 = new Part(3,"333",7);
var p4 = new Part(4,"443",3);
var p5 = new Part(5,"553",1);
var p6 = new Part(6,"663",8);
var p7 = new Part(7,"773",1);
var Assy1 = new BOM(1,'BIG' , 2);
var Assy2 = new BOM(2,'SUBAS' ,3);
Assy1.add(p1);
Assy1.add(p2);
Assy1.add(p3);
Assy2.add(p6);
Assy2.add(p7);
Assy1.add(Assy2);
Assy1.add(p4);
Assy1.add(p5);
document.write(Assy1.asXML());
